/*
 * Copyright 2011 Angel Sanadinov
 *
 * This file is part of VBox WMI.
 *
 * VBox WMI is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * VBox WMI is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with VBox WMI.  If not, see <http://www.gnu.org/licenses/>.
 */

package Beans.Requests.NetworkManagement;

import Beans.Requests.NetworkManagement.Components.DHCPServerConfiguration;
import Utilities.Constants.NetworkType;
import Utilities.Functions;
import Utilities.PermissionsConstants;

/**
 * Instances of this class are used by the user interface when a request to
 * create a DHCP server is sent to the networks management service.
 *
 * @author Angel Sanadinov
 */
public class CreateDHCPServerRequest extends NetworkManagementRequest
{
    private NetworkType networkType; //the type of network the DHCP is for
    private DHCPServerConfiguration serverConfiguration; //DHCP server configuration
    private boolean enableDHCPServer; //the state of the DHCP server

    /**
     * No-argument constructor. <br><br>
     * All fields are set to their invalid values.
     *
     * @see NetworkType#INVALID_TYPE
     */
    public CreateDHCPServerRequest()
    {
        super();
        this.networkType = NetworkType.INVALID_TYPE;
        this.serverConfiguration = null;
        this.enableDHCPServer = false;
        setRequestedAction(PermissionsConstants.ACTION_NETWORK_CREATE_DHCP);
    }

    /**
     * Constructs a "create DHCP" request with the specified data. <br><br>
     *
     * <b>Note:</b> <i>The DHCP configuration needs to be explicitly set!</i>
     *
     * @param requestorId the id of the user that made the request
     * @param serverId the id of the server on which the network resides
     * @param networkId the id of the network for which the DHCP is created
     * @param networkType the type of the target network
     */
    public CreateDHCPServerRequest(int requestorId, int serverId, String networkId, NetworkType networkType)
    {
        this(requestorId, serverId, networkId, networkType, null, false);
    }

    /**
     * Constructs a create DHCP request with the specified data.
     *
     * @param requestorId the id of the user that made the request
     * @param serverId the id of the server on which the network resides
     * @param networkId the id of the network for which the DHCP is created
     * @param networkType the type of the target network
     * @param serverConfiguration DHCP server configuration
     * @param enableDHCPServer DHCP server state (enabled/disabled)
     */
    public CreateDHCPServerRequest(int requestorId, int serverId, String networkId, NetworkType networkType,
                                   DHCPServerConfiguration serverConfiguration, boolean enableDHCPServer)
    {
        setRequestorId(requestorId);
        setServerId(serverId);
        setNetworkId(networkId);
        setRequestedAction(PermissionsConstants.ACTION_NETWORK_CREATE_DHCP);
        this.networkType = networkType;
        this.serverConfiguration = serverConfiguration;
        this.enableDHCPServer = enableDHCPServer;
    }

    /**
     * Retrieves the type of the target network.
     *
     * @return the network type
     */
    public NetworkType getNetworkType()
    {
        return networkType;
    }

    /**
     * Retrieves the DHCP server configuration.
     *
     * @return the DHCP configuration, if available or <code>null</code> otherwise
     */
    public DHCPServerConfiguration getServerConfiguration()
    {
        return serverConfiguration;
    }

    /**
     * Retrieves the state of the DHCP server.
     *
     * @return <code>true</code> if the DHCP server is to be enabled or <code>false</code>
     *         if it is to be disabled
     */
    public boolean getDHCPServerState()
    {
        return enableDHCPServer;
    }

    /**
     * Sets the target network's type.
     *
     * @param networkType the network type
     */
    public void setNetworkType(NetworkType networkType)
    {
        this.networkType = networkType;
    }

    /**
     * Sets the DHCP server configuration.
     *
     * @param serverConfiguration the DHCP server configuration
     */
    public void setServerConfiguration(DHCPServerConfiguration serverConfiguration)
    {
        this.serverConfiguration = serverConfiguration;
    }

    /**
     * Sets the state of the DHCP server.
     *
     * @param enableDHCPServer set to <code>true</code> if the DHCP server is to
     *                         be enabled or to <code>false</code> if it is to be
     *                         disabled
     */
    public void setDHCPServerState(boolean enableDHCPServer)
    {
        this.enableDHCPServer = enableDHCPServer;
    }

    @Override
    public boolean isValid()
    {
        if(super.isValid() && networkType != null && networkType != NetworkType.INVALID_TYPE
                && (networkType == NetworkType.HOST_ONLY || Functions.isUUIDValid(getNetworkId()))
                && (serverConfiguration == null || serverConfiguration.isValid()))
            return true;
        else
            return false;
    }

    /**
     * Returns a textual representation of the object. <br>
     *
     * It is in the form: "ClassName: (networkId,networkType,
     * [ipAddress,mask,rangeLow,rangeHigh|null],serverState)".
     *
     * @return the object represented as a String
     */
    @Override
    public String toString()
    {
        String dhcpSettings = null;

        if(serverConfiguration != null)
        {
            dhcpSettings = serverConfiguration.getServerIPAddress()
                           + "," + serverConfiguration.getServerNetworkMask()
                           + "," + serverConfiguration.getAddressRangeLow()
                           + "," + serverConfiguration.getAddressRangeHigh()
                           + "," + serverConfiguration.getServerState();
        }
        else
            ;

        return CreateDHCPServerRequest.class.getSimpleName() + ": (" + getNetworkId()
                + "," + networkType + "," + dhcpSettings + "," + enableDHCPServer + ")";
    }
}
